package com.example.demo111.handler;

import com.example.demo111.entity.Project;
import com.example.demo111.entity.UserInfo;
import com.example.demo111.entity.Record;
import com.example.demo111.service.ProjectService;
import com.example.demo111.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;


@Controller
//专门跳页面
public class JumpPageController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    ProjectService projectService;

    @RequestMapping("adminpassword")
    public String adminpassword(Model model,String name,String pwd,String newpwd,String agepwd,String iserror1) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        if(null==iserror1||"false".equals(iserror1))return "admin/password";
        userInfoService.updatePassword(name,pwd,newpwd);
        System.out.println(name+pwd+newpwd+agepwd);
        return "admin/password";
    }
    @RequestMapping("admin")
    public String admin(Model model) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        System.out.println(user);
        return "admin";
    }
    @RequestMapping("admininfo")
    public String getAdminList(Model model) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("List", user);
        model.addAttribute("auser", user.getName());
        System.out.println(user);
        return "admin/admininfo";
    }

    @RequestMapping("userinfo")
    public String userinfo(Model model) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("List", user);
        model.addAttribute("auser", user.getName());
        return "user/userinfo";
    }

    @RequestMapping("userpassword")
    public String userpassword(Model model,String name,String pwd,String newpwd,String agepwd,String iserror1) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        if(null==iserror1||"false".equals(iserror1))return "user/password";
        userInfoService.updatePassword(name,pwd,newpwd);

        return "user/password";
    }

    @RequestMapping("uservoteinfo")
    public String uservoteinfo(Model model) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        return "user/uservoteinfo";
    }

    @RequestMapping("user")
    public String user(Model model) {
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        System.out.println(user);
        return "user";
    }

    @RequestMapping("aboutUs")
    public String aboutUs() {
        return "aboutUs";
    }

    @RequestMapping("Index")
    public String Index(Model model,String pname) {
        if(pname!=null){
            List<Project> pro =this.projectService.getVoteItem(pname);
            model.addAttribute("pRo", pro);
            return "voteProject";
        }
        List<Project> proList = this.projectService.getItem();
        model.addAttribute("pRo", proList);
        return "Index";
    }


    @RequestMapping("MesAboutStudy")
    public String MesAboutStudy() {
        return "MesAboutStudy";
    }



    @RequestMapping("ProjectVote")
    public String ProjectVote(Model model,Integer id,Integer pid) {
        if(pid!=null){
            UserInfo user = this.userInfoService.search();
            Record record = userInfoService.selectVoteInfo(pid, user.getUid());
            if (record!=null){
                if(id!=null){
                    Project p = this.projectService.getProId(id);
                    model.addAttribute("pR",p);
                    if(p.getTno()==1){model.addAttribute("pRl","关于学习");}
                    if(p.getTno()==2){model.addAttribute("pRl","关于住宿");}
                    if(p.getTno()==3){model.addAttribute("pRl","关于食堂");}
                    if(p.getTno()==4){model.addAttribute("pRl","关于生活");}
                    return "ProjectVote";
                }
            }
            this.userInfoService.userVote(pid, user.getUid());
            this.userInfoService.VoteAdd(pid);
            if(id!=null){
                Project p = this.projectService.getProId(id);
                model.addAttribute("pR",p);
                if(p.getTno()==1){model.addAttribute("pRl","关于学习");}
                if(p.getTno()==2){model.addAttribute("pRl","关于住宿");}
                if(p.getTno()==3){model.addAttribute("pRl","关于食堂");}
                if(p.getTno()==4){model.addAttribute("pRl","关于生活");}
                return "ProjectVote";
            }
        }
        if(id!=null){
            Project p = this.projectService.getProId(id);
            model.addAttribute("pR",p);
            if(p.getTno()==1){model.addAttribute("pRl","关于学习");}
            if(p.getTno()==2){model.addAttribute("pRl","关于住宿");}
            if(p.getTno()==3){model.addAttribute("pRl","关于食堂");}
            if(p.getTno()==4){model.addAttribute("pRl","关于生活");}
            return "ProjectVote";
        }
        Project pr = new Project(1,"1",1,"1",1,"1","1","1");
        model.addAttribute("pR",pr);
        return "ProjectVote";
    }

    @RequestMapping("adminproject")
    public String Adminproject(Model model,String pname) {
        if(pname!=null){
            List<Project> pro =this.projectService.getVoteItem(pname);
            UserInfo user = this.userInfoService.search();
            model.addAttribute("auser", user.getName());
            model.addAttribute("pRo", pro);
            model.addAttribute("pageNum",pro.size());
            return "admin/adminproject";
        }

        List<Project> proList = this.projectService.getItem();
        UserInfo user = this.userInfoService.search();
        model.addAttribute("auser", user.getName());
        model.addAttribute("pRo", proList);
        model.addAttribute("pageNum", proList.size());
        return "admin/adminproject";
    }

    @RequestMapping("del")
    public ModelAndView del(Integer pid){
        projectService.deletePro(pid);
        return new ModelAndView("redirect:adminproject");
    }


    @RequestMapping("voteProject")
    public String voteProject(Model model,String pname,Integer project) {

        if(pname!=null){
            List<Project> pro =this.projectService.getVoteItem(pname);
            model.addAttribute("pRo", pro);
            return "voteProject";
        }
        if(project!=null){
            List<Project> pro =this.projectService.getCatagory(project);
            model.addAttribute("pRo", pro);
            return "voteProject";
        }
        List<Project> proList = this.projectService.getItem();
        model.addAttribute("pRo", proList);
        return "voteProject";

    }

    @RequestMapping("add")
    public ModelAndView addPage(Model model){
        model.addAttribute("project",new Project());
        return new ModelAndView("admin/addPage","promodel",model);
    }
    @RequestMapping("add1")
    public ModelAndView add(Project project){
        if (Objects.equals(project.getPname(), "")){
            return new ModelAndView("redirect:/add");
        }else {
        projectService.Add(project);
        return new ModelAndView("redirect:/adminproject");
        }
    }
}
